package com.ham.mud.characters.mobs;

import com.ham.mud.characters.Experience;
import com.ham.mud.characters.npcs.NPC;
import com.ham.mud.characters.npcs.QuestMasterNPC;
import com.ham.mud.characters.npcs.VendorNPC;
import com.ham.mud.classes.MudClassService;
import com.ham.mud.configuration.FilePaths;
import com.ham.mud.items.EquipSlot;
import com.ham.mud.items.Item;
import com.ham.mud.items.ItemService;
import com.ham.mud.races.Human;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by hlucas on Jun 16, 2011 at 4:24:23 PM
 */
public class MobService {
    private static final Random rand = new Random();

    public static List<Mob> getMobs(String zone, String[] mobNames) {
        List<Mob> mobs = new ArrayList<Mob>();
        for(String mobName : mobNames) {
            if(mobName == null || mobName.trim().equals("")) continue;
            try {
                Mob mob = loadMob(zone, mobName);
                mobs.add(mob);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mobs;
    }

    public static Mob getMob(String zone, String mobName) {
        try {
            return loadMob(zone, mobName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // name
    // shortDesc
    // longDesc
    // targeting keys
    // item names
    // equipment names
    // class and experience

    private static Mob loadMob(String zone, String mobName) throws IOException {
        File file = new File(FilePaths.getMobFilePath(zone, mobName));
        FileInputStream inputStream = new FileInputStream(file);

        DataInputStream in = new DataInputStream(inputStream);

        String name = in.readLine();
        String shortDesc = in.readLine();
        String longDesc = in.readLine();
        String[] targetingKeys = in.readLine().split(" ");
        String[] itemNames = in.readLine().split(" ");
        String[] equipmentNames = in.readLine().split(" ");
        String[] classAndExperience = in.readLine().split(" ");

        in.close();
        inputStream.close();

        Mob mob;

        String npcType = classAndExperience[0];
        if(npcType.equals("VENDOR")) {
            mob = new VendorNPC(name, shortDesc, longDesc, targetingKeys);
            mob.setRace(new Human());
        } else if(npcType.equals("QUESTMASTER")) {
            mob = new QuestMasterNPC(name, shortDesc, longDesc, targetingKeys);
            mob.setRace(new Human());
        } else {
            mob = new Mob(name, shortDesc, longDesc, targetingKeys);
            mob.setRace(new Human());

            String className = classAndExperience[0];
            int experienceAmount = Integer.valueOf(classAndExperience[1]);

            mob.setMudClass(MudClassService.getClass(className));
            mob.setExperience(new Experience(experienceAmount));
        }

        List<Item> items = ItemService.getItems(itemNames);
        mob.setInventory(items);

        Map<EquipSlot, Item> equipment = ItemService.getEquipment(equipmentNames);
        mob.setEquipment(equipment);

        mob.setFileName(mobName);
        mob.setZone(zone);

        return mob;
    }

    public static Mob getRandomMob() {
        Mob testMob = getMob();
        if(testMob == null || testMob instanceof NPC) {
            return getRandomMob();
        }
        return testMob;
    }

    private static Mob getMob() {
        File zonesDir = new File(FilePaths.getZonesFilePath());
        File[] zones = zonesDir.listFiles();
        File zone = zones[rand.nextInt(zones.length)];
        File mobsDir = new File(zone.getAbsolutePath() + File.separator + "mobs");
        File[] mobs = mobsDir.listFiles();
        if(mobs == null || mobs.length == 0) {
            return null;
        }
        File randMob = mobs[rand.nextInt(mobs.length)];
        if(randMob.isDirectory()) return getMob(); //stupid .svn directory
        String mobName = randMob.getName();
        return getMob(zone.getName(), mobName.split("\\.")[0]);
    }
}
